<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Introduction - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.cache.introduction.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.cache.introduction.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.cache.html">Zend_Cache</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.cache.html">Zend_Cache</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.cache.theory.html">The Theory of Caching</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.cache.introduction" class="section"><div class="info"><h1 class="title">Introduction</h1></div>
    

    <p class="para">
        <span class="classname">Zend_Cache</span> provides a generic way to cache any data.
    </p>

    <p class="para">
      Caching in Zend Framework is operated by frontends while cache records are stored through
      backend adapters (<em class="emphasis">File</em>, <em class="emphasis">Sqlite</em>,
      <em class="emphasis">Memcache</em>...) through a flexible system of IDs and tags. Using those, it
      is easy to delete specific types of records afterwards (for example: &quot;delete all cache records
      marked with a given tag&quot;).
    </p>

    <p class="para">
        The core of the module (<span class="classname">Zend_Cache_Core</span>) is generic, flexible and
        configurable. Yet, for your specific needs there are cache frontends that extend
        <span class="classname">Zend_Cache_Core</span> for convenience: <em class="emphasis">Output</em>,
        <em class="emphasis">File</em>, <em class="emphasis">Function</em> and <em class="emphasis">Class</em>.
    </p>

    <div class="example" id="zend.cache.introduction.example-1"><div class="info"><p><b>Example #1 Getting a Frontend with Zend_Cache::factory()</b></p></div>
        

        <div class="example-contents"><p>
             <span class="methodname">Zend_Cache::factory()</span> instantiates correct objects and ties
            them together. In this first example, we will use <em class="emphasis">Core</em> frontend
            together with <em class="emphasis">File</em> backend.
        </p></div>

        <pre class="programlisting brush: php">
$frontendOptions = array(
   &#039;lifetime&#039; =&gt; 7200, // cache lifetime of 2 hours
   &#039;automatic_serialization&#039; =&gt; true
);

$backendOptions = array(
    &#039;cache_dir&#039; =&gt; &#039;./tmp/&#039; // Directory where to put the cache files
);

// getting a Zend_Cache_Core object
$cache = Zend_Cache::factory(&#039;Core&#039;,
                             &#039;File&#039;,
                             $frontendOptions,
                             $backendOptions);
</pre>

    </div>

    <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Frontends and Backends Consisting of Multiple Words</b><br /></span>
        

        <p class="para">
            Some frontends and backends are named using multiple words, such
            as &#039;ZendPlatform&#039;. When specifying them to the factory, separate
            them using a word separator, such as a space (&#039; &#039;), hyphen
            (&#039;-&#039;), or period (&#039;.&#039;).
        </p>
    </p></blockquote>

    <div class="example" id="zend.cache.introduction.example-2"><div class="info"><p><b>Example #2 Caching a Database Query Result</b></p></div>
        

        <div class="example-contents"><p>
            Now that we have a frontend, we can cache any type of data (we turned on serialization).
            for example, we can cache a result from a very expensive database query. After it is
            cached, there is no need to even connect to the database; records are fetched from cache
            and unserialized.
        </p></div>

        <pre class="programlisting brush: php">
// $cache initialized in previous example

// see if a cache already exists:
if( ($result = $cache-&gt;load(&#039;myresult&#039;)) === false ) {

    // cache miss; connect to the database

    $db = Zend_Db::factory( [...] );

    $result = $db-&gt;fetchAll(&#039;SELECT * FROM huge_table&#039;);

    $cache-&gt;save($result, &#039;myresult&#039;);

} else {

    // cache hit! shout so that we know
    echo &quot;This one is from cache!\n\n&quot;;

}

print_r($result);
</pre>

    </div>

    <div class="example" id="zend.cache.introduction.example-3"><div class="info"><p><b>Example #3 Caching Output with Zend_Cache Output Frontend</b></p></div>
        

        <div class="example-contents"><p>
            We &#039;mark up&#039; sections in which we want to cache output by adding some conditional logic,
            encapsulating the section within  <span class="methodname">start()</span> and
             <span class="methodname">end()</span> methods (this resembles the first example and is the core
            strategy for caching).
        </p></div>

        <div class="example-contents"><p>
            Inside, output your data as usual - all output will be cached when execution hits the
             <span class="methodname">end()</span> method. On the next run, the whole section will be
            skipped in favor of fetching data from cache (as long as the cache record is valid).
        </p></div>

        <pre class="programlisting brush: php">
$frontendOptions = array(
   &#039;lifetime&#039; =&gt; 30,                   // cache lifetime of 30 seconds
   &#039;automatic_serialization&#039; =&gt; false  // this is the default anyways
);

$backendOptions = array(&#039;cache_dir&#039; =&gt; &#039;./tmp/&#039;);

$cache = Zend_Cache::factory(&#039;Output&#039;,
                             &#039;File&#039;,
                             $frontendOptions,
                             $backendOptions);

// we pass a unique identifier to the start() method
if(!$cache-&gt;start(&#039;mypage&#039;)) {
    // output as usual:

    echo &#039;Hello world! &#039;;
    echo &#039;This is cached (&#039;.time().&#039;) &#039;;

    $cache-&gt;end(); // the output is saved and sent to the browser
}

echo &#039;This is never cached (&#039;.time().&#039;).&#039;;
</pre>


        <div class="example-contents"><p>
            Notice that we output the result of  <span class="methodname">time()</span> twice; this is
            something dynamic for demonstration purposes. Try running this and then refreshing
            several times; you will notice that the first number doesn&#039;t change while second changes
            as time passes. That is because the first number was output in the cached section and is
            saved among other output. After half a minute (we&#039;ve set lifetime to 30 seconds) the
            numbers should match again because the cache record expired -- only to be cached again.
            You should try this in your browser or console.
        </p></div>
    </div>

    <blockquote class="note"><p><b class="note">Note</b>: 
        <p class="para">
            When using <span class="classname">Zend_Cache</span>, pay attention to the important cache
            identifier (passed to  <span class="methodname">save()</span> and
             <span class="methodname">start()</span>). It must be unique for every resource you cache,
            otherwise unrelated cache records may wipe each other or, even worse, be displayed in
            place of the other.
        </p>
    </p></blockquote>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.cache.html">Zend_Cache</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.cache.html">Zend_Cache</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.cache.theory.html">The Theory of Caching</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.cache.html">Zend_Cache</a></li>
  <li class="active"><a href="zend.cache.introduction.html">Introduction</a></li>
  <li><a href="zend.cache.theory.html">The Theory of Caching</a></li>
  <li><a href="zend.cache.frontends.html">Zend_Cache Frontends</a></li>
  <li><a href="zend.cache.backends.html">Zend_Cache Backends</a></li>
  <li><a href="zend.cache.cache.manager.html">The Cache Manager</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>